<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item label="用户昵称">
          <el-input
            v-model="listQuery.nickname"
            placeholder="请输入用户昵称"
          ></el-input>
        </el-form-item>

        <el-form-item label="申请日期">
          <el-date-picker
            v-model="listQuery.create_time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search" size="small"
            >查询</el-button
          >
          <el-button @click="reset" size="small">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="downloadLoading"
            type="success"
            icon="el-icon-download"
            @click="handleDownload"
            size="small"
          >
            数据导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      key="id"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
    >
      <el-table-column label="序号" type="index" width="50"> </el-table-column>
      <el-table-column label="用户昵称" prop="nickname"> </el-table-column>
      <el-table-column
        label="申请标题"
        prop="adopt.title"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="申请类型" prop="apply_type"> </el-table-column>
      <el-table-column label="申请语" prop="apply_term" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="申请时间" prop="create_time"> </el-table-column>

      <el-table-column
        label="操作"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row, $index }">
          <el-button type="text" size="mini" @click="handleView(row)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <el-dialog width="500px" title="详细信息" :visible.sync="showDialog">
      <el-form
        class="modal-scroll"
        :model="info"
        label-position="left"
        label-width="70px"
      >
        <el-form-item label="用户昵称">{{ info.nickname }}</el-form-item>
        <el-form-item label="用户性别"> {{ info.sex_text }} </el-form-item>
        <el-form-item label="婚姻状况"> {{ info.marital }} </el-form-item>
        <el-form-item label="住房情况"> {{ info.house }} </el-form-item>
        <el-form-item label="出生日期"> {{ info.birth }} </el-form-item>
        <el-form-item label="职业"> {{ info.job }} </el-form-item>
        <el-form-item label="手机"> {{ info.mobile }} </el-form-item>
        <el-form-item label="微信"> {{ info.wechat }} </el-form-item>
        <el-form-item label="地址"> {{ info.location }} </el-form-item>
        <el-form-item label="申请标题">
          {{ info.adopt && info.adopt.title }}
        </el-form-item>
        <el-form-item label="养宠经验"> {{ info.experience }} </el-form-item>
        <el-form-item label="申请语"> {{ info.apply_term }} </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, exportExcel, getInfo } from "@/api/adopt_applys";
import Pagination from "@/components/Pagination";

export default {
  name: "applyFor",
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        nickname: undefined,
        create_time: undefined,
      },
      info: {},
      showDialog: false,
      downloadLoading: false,
    };
  },
  mounted() {
    this.getList();
  },

  methods: {
    async getList() {
      this.listLoading = true;
      const res = await fetchList(this.listQuery);
      if (res.data) {
        this.list = res.data.list;
        this.total = res.data.pageParam.total;
      }
      this.listLoading = false;
    },
    // 查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },

    reset() {
      this.listQuery = {
        page: 1,
        pageSize: 20,
        nickname: undefined,
        create_time: undefined,
      };
      this.getList();
    },
    async fetchInfo(id) {
      const res = await getInfo({ id });
      if (res.data) {
        this.info = res.data;
      }
    },
    // 查看资料
    handleView(row) {
      this.fetchInfo(row.id);
      this.showDialog = true;
    },
    // 数据导出
    async handleDownload() {
      this.downloadLoading = true;
      const res = await exportExcel(this.listQuery);
      if (res) {
        this.downloadFile(res, "领养申请列表");
      }
      this.downloadLoading = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.modal-scroll {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
